package com.chaoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.ChannelSwitch;
import com.chaoyi.model.Classification;
import com.chaoyi.service.ChannelSwitchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 卜加超
 * @Date: 2019/1/3 09:53
 * @Description:
 */
@Controller
@RequestMapping("channelSwitch")
public class ChannelSwitchController {

    private Logger log = LoggerFactory.getLogger(ChannelSwitchController.class);

    @Resource
    private ChannelSwitchService channelSwitchService;
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ModelAndView mod = new ModelAndView("/channelSwitch/channelSwitchList");
        return mod;
    }
    @RequestMapping("/channelSwitchList")
    @ResponseBody
    public JSONObject channelSwitchList(int offset, int limit, ChannelSwitch channelSwitch) {
        JSONObject json = new JSONObject();

        int count = channelSwitchService.findCount(channelSwitch);

        List<ChannelSwitch> list = new ArrayList<ChannelSwitch>();
        if (count > 0) {
            list = channelSwitchService.findList(offset, limit, channelSwitch);
        }
        json.put("rows", list);
        json.put("total", count);
        return json;
    }

    /**
     * 跳转到添加分类
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/add_plus")
    public ModelAndView add_plus(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/channelSwitch/channelSwitch_add");
        return mv;
    }
    /**
     * 添加分类
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("channelSwitchAdd")
    public JSONObject addChannelSwitchSave(ChannelSwitch entity) {
        JSONObject json = new JSONObject();
        int number = channelSwitchService.addChannelSwitchSave(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }
    /**
     * 跳转到修改页面
     */
    @RequestMapping("/alertChannelSwitch")
    public ModelAndView alertVideo(HttpServletRequest request, HttpServletResponse response,int id) {
        ModelAndView mod = new ModelAndView("/channelSwitch/channelSwitch_update");
        ChannelSwitch entity = channelSwitchService.findChannelSwitchOne(id);
        System.out.println(entity.toString());
        mod.addObject("obj",entity);
        return mod;
    }
    @ResponseBody
    @RequestMapping("edit")
    public JSONObject alertClassificationSave(HttpServletRequest request, HttpServletResponse response, ChannelSwitch entity) {
        JSONObject json = new JSONObject();
        int number = channelSwitchService.edit(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    /**
     * 删除分类
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteChannelSwitch")
    public JSONObject deleteClassification(HttpServletRequest request, HttpServletResponse response, int id) {
        JSONObject json = new JSONObject();
        log.info("id="+id);
        int number = channelSwitchService.deleteChannelSwitch(id);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }


}
